﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;               //USING MYSQL CONNECTOR
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Add_Product : Form
    {
        public Add_Product()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox_name.Text = "";
            this.textBox_price.Text = "";
            this.textBox_productid.Text = "";
            this.textBox_quantity.Text = "";
            status.Text = "Reset";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool validation = true;

            if (textBox_name.Text.Equals(null))
            {
                validation = false;
            }

            if (validation == true)
            {
                
                //Getting Database Info
                load_db databaseinfo = new load_db();
                

                //MYSQL Initializations
                //string connString = string.Format("server=localhost;port=3306;database=seniordesign;user id=root");
                string connString = string.Format("server={0};port=3306;database={1};user id={2};password={3}",
                databaseinfo.database_host, databaseinfo.database_name, databaseinfo.database_username, databaseinfo.datbase_password);

                MySqlConnection conn = new MySqlConnection(connString);
                MySqlDataReader reader = null;

                //After all the Validation are tested, we can send the data to the database
                try
                {
                    conn = new MySqlConnection(connString);
                    conn.Open();
            
                }
                catch (MySqlException ex)
                {
                    this.status.Text = "Error in Database Connection" + ex.Message;
                }

                
                string queryString = "INSERT INTO  `" + databaseinfo.database_name + "`.`product` (`PID` ,`NAME` ,`PRICE` ,`QUANTITY`, `TotalSales`)VALUES ('" + textBox_productid.Text.ToString() + "','" + textBox_name.Text.ToString() + "','" + textBox_price.Text.ToString() + "','" + textBox_quantity.Text.ToString() + "', '0');";
                MySqlCommand cmd = new MySqlCommand(queryString, conn);

                try
                {
                    reader = cmd.ExecuteReader();

                    this.status.Text = "Product Added to the System";
                    this.textBox_productid.Text = "";
                    this.textBox_name.Text = "";
                    this.textBox_price.Text = "";
                    this.textBox_quantity.Text = "";


                }

                catch (Exception ex)
                {
                    statusStrip_addProduct.Text = "Error Inserting New Product" + ex.Message;
                }

                finally
                {
                    if (reader != null) reader.Close();
                }
            }

        }

        private void textBox_productid_TextChanged(object sender, EventArgs e)
        {

        }

        private void Add_Product_Load(object sender, EventArgs e)
        {
            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
